package com.server.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.server.authority.Login;
import com.server.common.base.ProjectInfor;
import com.server.common.base.SessionData;
import com.server.service.ICommonDataService;
import com.server.service.IMngTrainOrgService;
import com.server.web.DataVector;
import com.server.web.JSONSerielizer;
import com.server.web.OutputUtil;
import com.server.web.RequestInput;
import com.server.web.SessionUtil;

/**
 * 受训机构管理
 * @author ServerZhang
 * @date 2016年12月1日
 */
@Controller
@RequestMapping("trainOrg")
public class MngTrainOrgController {
	
	private String tableName = "education.trainorg";
	
	@Autowired
	private IMngTrainOrgService mngTrainOrgService;
	@Autowired
	private ICommonDataService commonDataService;
	
	@Login
	@RequestMapping("")
	public ModelAndView orgList(HttpServletRequest request,
			HttpServletResponse response){
		ModelAndView mv = new ModelAndView("trainOrg/orgList");
		mv.addObject("rootPath", ProjectInfor.ROOT_PATH);
		return mv;
	}
	@Login
	@RequestMapping("list")
	public void list(HttpServletRequest request,
			HttpServletResponse response) {
		String _whereStr = "valid=1";
		String sortStr = "createTime desc";
		DataVector<Map<String, Object>> listData = commonDataService.getListData(tableName, _whereStr, sortStr, new RequestInput(request));
		OutputUtil.returnGridData(response, listData.getCount(), JSONSerielizer.toJson(listData.getList(), null, null));
	}
	@Login
	@RequestMapping("form/{id}")
	public void form(HttpServletRequest request,
			HttpServletResponse response,@PathVariable String id){
		Map<String, Object> formData = commonDataService.getFormData(tableName, "id", id, null, null);
		OutputUtil.returnString(response, JSONSerielizer.toJson(formData, null, null));
	}
	@Login
	@RequestMapping("check/{id}/{state}/{type}")
	public ModelAndView check(HttpServletRequest request,
			HttpServletResponse response,@PathVariable Integer id,
			@PathVariable Integer state,@PathVariable Integer type) {
		String userName = SessionUtil.getStringAttri(request, SessionData.nameKey_mng);
		ModelAndView mv = new ModelAndView("/common/editFrame");
		mv.addObject("pagePath","pages/trainOrg/orgForm.jsp");
		mv.addObject("rootPath",ProjectInfor.ROOT_PATH);
		mv.addObject("action", "check");
		mv.addObject("state", state);
		mv.addObject("type", type);
		mv.addObject("business", "trainOrg");
		mv.addObject("titleName", "查看受训机构");
		mv.addObject("userName", userName);
		return mv;
	}
	@Login
	@RequestMapping("add/{type}")
	public ModelAndView add(HttpServletRequest request,
			HttpServletResponse response,@PathVariable Integer type) {
		String userName = SessionUtil.getStringAttri(request, SessionData.nameKey_mng);
		ModelAndView mv = new ModelAndView("/common/editFrame");
		mv.addObject("pagePath","pages/trainOrg/orgForm.jsp");
		mv.addObject("rootPath",ProjectInfor.ROOT_PATH);
		mv.addObject("action", "new");
		mv.addObject("type",type);
		mv.addObject("business", "trainOrg");
		mv.addObject("titleName", "新增受训机构");
		mv.addObject("userName", userName);
		return mv;
	}
	@RequestMapping("save/{id}")
	public void save(HttpServletRequest request,
			HttpServletResponse response,@PathVariable Integer id){
		String operator = SessionUtil.getStringAttri(request, SessionData.nameKey_mng);
		String formData = request.getParameter("formData");
		Map<String, Object> saveMap = JSONSerielizer.toMap(formData);
		Boolean isSuccess = true;
		String info = null;
		if (id.equals(0)) {
			/*String nickname  = saveMap.get("nickname").toString(); 
			isSuccess = mngTrainOrgService.check(nickname);*/
			if(isSuccess){
				isSuccess = commonDataService.add(operator, tableName, saveMap);
			}else{
				info = "受训机构已存在";
			}
			
		}else {
			isSuccess = commonDataService.update(operator, tableName, "id", id.toString(), saveMap);
		}
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("isSuccess", isSuccess);
		map.put("info", info);
		OutputUtil.returnString(response, JSONSerielizer.toJson(map, null, null));
	}
	@Login
	@RequestMapping("update/{id}/{state}/{type}")
	public ModelAndView update(HttpServletRequest request,
			HttpServletResponse response,@PathVariable Integer id,
			@PathVariable Integer state,@PathVariable Integer type) {
		String userName = SessionUtil.getStringAttri(request, SessionData.nameKey_mng);
		ModelAndView mv = new ModelAndView("/common/editFrame");
		mv.addObject("pagePath","pages/trainOrg/orgForm.jsp");
		mv.addObject("rootPath",ProjectInfor.ROOT_PATH);
		mv.addObject("action", "edit");
		mv.addObject("type", type);
		mv.addObject("state", state);
		mv.addObject("business", "trainOrg");
		mv.addObject("titleName", "编辑受训机构信息");
		mv.addObject("userName", userName);
		return mv;
	}
	@RequestMapping("delete")
	public void delete(HttpServletRequest request,
			HttpServletResponse response){
		String ids = request.getParameter("ids");
		Integer estimate = mngTrainOrgService.deleteAdminUser(ids);
		OutputUtil.returnString(response, estimate.toString());
	}
	@RequestMapping("roleList")
	public void roleList(HttpServletRequest request,HttpServletResponse response){
		List<Map<String, Object>> list = mngTrainOrgService.roleList();
		OutputUtil.returnString(response, JSONSerielizer.toJson(list, null, null));
	}
	@RequestMapping("getOrg")
	public void getOrg(HttpServletRequest request,HttpServletResponse response){
		List<Map<String, Object>> list = mngTrainOrgService.getOrg();
		OutputUtil.returnString(response, JSONSerielizer.toJson(list, null, null));
	}
	@RequestMapping("getField")
	public void getField(HttpServletRequest request,HttpServletResponse response){
		List<Map<String, Object>> list = mngTrainOrgService.getField();
		OutputUtil.returnString(response, JSONSerielizer.toJson(list, null, null));
	}
}
